<template>
  <view class="tab-container">
    <scroll-view 
      class="scroll-wrapper"
      scroll-x
      :scroll-left="scrollLeft"
      scroll-with-animation
      show-scrollbar="false"
    >
      <view class="tab-list">
        <view 
          v-for="(tab, index) in tabs" 
          :key="index"
          class="tab-item"
          :class="{ 'active': activeIndex === index }"
          @click="handleTabClick(index)"
          :id="'tab_' + index"
        >
          <text class="tab-text">{{ tab.label }}</text>
          <view v-if="activeIndex === index" class="active-line"></view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  props: {
    tabs: {
      type: Array,
      default: () => []
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  name:"TabLayout",
  data() {
    return {
      scrollLeft: 0,
      tabWidths: []
    }
  },
  watch: {
    activeIndex: {
      handler(newVal) {
        this.$nextTick(() => {
          this.scrollToCenter(newVal)
        })
      },
      immediate: true
    }
  },
  methods: {
    handleTabClick(index) {
      this.$emit('change', index)
    },
    
    async scrollToCenter(index) {
      const query = uni.createSelectorQuery().in(this)
      query.select('.scroll-wrapper').boundingClientRect()
      query.select(`#tab_${index}`).boundingClientRect()
      
      query.exec(res => {
        const [scrollView, tab] = res
        if (!scrollView || !tab) return
        
        // 计算修正：考虑容器padding和当前滚动位置
        const containerPadding = 32 // 与.tab-list的padding一致
        const tabOffset = tab.left - scrollView.left + this.scrollLeft
        const scrollOffset = tabOffset - (scrollView.width / 2) + (tab.width / 2) - containerPadding
        
        // 限制滚动范围
        const maxScroll = scrollView.scrollWidth - scrollView.width
        this.scrollLeft = Math.max(0, Math.min(scrollOffset, maxScroll))
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-list {
  // 添加过渡效果
  transition: transform 0.3s ease;
  
  .tab-item {
    // 添加点击反馈
    &:active {
      opacity: 0.7;
    }
  }
}
</style>

<style lang="scss" scoped>
.tab-container {
  height: 88rpx;
  background-color: #fff;
  position: relative;
  
  .scroll-wrapper {
    height: 100%;
    white-space: nowrap;
    
    .tab-list {
      display: inline-flex;
      height: 100%;
      padding: 0 32rpx;
      
      .tab-item {
        position: relative;
        padding: 0 32rpx;
        height: 100%;
        display: inline-flex;
        align-items: center;
        flex-shrink: 0;
        
        .tab-text {
          font-size: 30rpx;
          color: #666;
          transition: color 0.3s;
        }
        
        &.active {
          .tab-text {
            color: #248CFA;
            font-weight: 500;
          }
          
          .active-line {
            position: absolute;
            bottom: 8rpx;
            left: 50%;
            transform: translateX(-50%);
            width: 64rpx;
            height: 4rpx;
            background-color: #248CFA;
            border-radius: 2rpx;
          }
        }
      }
    }
  }
}
</style>